//
//  SEGBillSearchTableViewCell.m
//  SEGUhomePropertyBaseService
//
//  Created by Clavis on 2019/2/28.
//  Copyright © 2019 刘山. All rights reserved.
//

#import "SEGBillSearchTableViewCell.h"
#import "SEGBillSearchSubViewCell.h"
#import "SEGOwesMonthDetailModel.h"

@interface SEGBillSearchTableViewCell ()

@property (nonatomic, strong) UIView *contentBox;
@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) UIButton *switchButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *statusLabel;
@property (nonatomic, strong) UILabel *feeLabel;
@property (nonatomic, strong) UIImageView *arrowImage;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIView *feeDetailView;
@property (nonatomic, strong) SEGBillMonthModel *model;

@end

@implementation SEGBillSearchTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.contentBox = [[UIView alloc] init];
    self.contentBox.backgroundColor = kColor_W;
    [self.contentView addSubview:self.contentBox];
    [self.contentBox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(-5);
    }];
    
    self.titleView = [[UIView alloc] init];
    self.titleView.backgroundColor = UIColor.clearColor;
    [self.contentBox addSubview:self.titleView];
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_offset(0);
        make.height.mas_equalTo(55);
    }];
    
    self.titleLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        [self.titleView addSubview:lable];
        lable.textColor = kColor_1;
        lable.font = [UIFont boldSystemFontOfSize:15];
        lable;
    });
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_offset(0);
    }];
    
    self.statusLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        [self.titleView addSubview:lable];
        lable.textColor = kColor_S1;
        lable.font = kFont_11;
        lable.text = @"未缴清";
        lable;
    });
    [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right).mas_offset(10);
        make.bottom.equalTo(self.titleLabel.mas_bottom);
    }];
    
    self.arrowImage = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        [self.titleView addSubview:imageView];
        imageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_zhankai")];
        imageView;
    });
    [self.arrowImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_offset(0);
    }];
    
    self.feeLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        [self.titleView addSubview:lable];
        lable.textColor = kColor_1;
        lable.font = [UIFont boldSystemFontOfSize:15];
        lable;
    });
    [self.feeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-35);
        make.centerY.equalTo(self.arrowImage);
    }];
    
    self.lineView = [[UIView alloc] init];
    self.lineView.backgroundColor = kColor_L2;
    [self.titleView addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.height.mas_equalTo(0.5);
    }];
    
    self.switchButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(switchButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleView addSubview:button];
        button;
    });
    [self.switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
    
    self.feeDetailView = [[UIView alloc] init];
    [self.contentBox addSubview:self.feeDetailView];
    [self.feeDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleView.mas_bottom);
        make.left.right.bottom.offset(0);
    }];
    
    // 添加阴影
    self.contentBox.layer.shadowColor = [[UIColor colorWithHexString:@"#999999"] colorWithAlphaComponent:0.16].CGColor;;
    self.contentBox.layer.shadowOffset = CGSizeMake(0, 0);
    self.contentBox.layer.shadowOpacity = 1;
    self.contentBox.layer.shadowRadius = 5;
    self.contentBox.layer.cornerRadius = 5;
}

- (void)updateUIWithModel:(SEGBillMonthModel *)model {
    self.titleLabel.text = [NSString stringWithFormat:@"%ld月账单", (long)model.billMonth];
    self.feeLabel.text = [NSString stringWithFormat:@"￥%.2f", model.cycleAmount.doubleValue *MoneyRateChange];
    self.switchButton.selected = model.isSelect;
    self.arrowImage.image = [UIImage imageWithContentsOfFile:SEGPicName(model.isSelect ? @"btn_shouqi" : @"btn_zhankai")];
    self.lineView.hidden = !model.isSelect;
    self.statusLabel.text = model.payStatus == SEGBillDetailDoLogicModelFeeStatus_PayAll ? @"已缴清" : @"未缴清";
    self.statusLabel.textColor = model.payStatus == SEGBillDetailDoLogicModelFeeStatus_PayAll ? kColor_3 : kColor_S1;
    
    // 展开账单项
    [self.feeDetailView removeAllSubviews];
    if (model.isSelect) {
        UIView *lastView = nil;
        for (NSInteger i = 0; i < model.billDetails.count; i++) {
            SEGOwesMonthDetailModel *detailModel = model.billDetails[i];
            SEGBillSearchSubViewCell *subView = [[SEGBillSearchSubViewCell alloc] initWithModel:detailModel];
            [self.feeDetailView addSubview:subView];
            [subView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_offset(0);
                if (lastView) {
                    make.top.equalTo(lastView.mas_bottom);
                } else {
                    make.top.mas_equalTo(0);
                }
                if (i == model.billDetails.count - 1) {
                    [subView hideLine];
                    make.bottom.mas_equalTo(0);
                }
            }];
            lastView = subView;
        }
    }
}

- (void)switchButtonTap:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (self.switchBlcok) {
        self.switchBlcok(sender.selected);
    }
}

@end
